package com.study.userserviceprovider.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 此类描述的是：
 *
 * @author: 李军容
 * @version: 2020/11/14 20:11
 */
@RestController
public class UserController {


    @Autowired(required = false)
    private RestTemplate restTemplate;

  /*  @Autowired
    private LoadBalancerClient loadBalancerClient;
*/


    @GetMapping("/user/{id}")
    public String findById(@PathVariable("id") int id){
        //调用http协议
        //HttpClient RestTemplate OkHttp HttpUrlConnection
       // ServiceInstance serviceInstance = loadBalancerClient.choose("spring-cloud-order-service");
        //String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()+"/orders");
        //return  restTemplate.getForObject("http://spring-cloud-order-service/orders",String.class);

        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://spring-cloud-order-service/orders",String.class);
        if(responseEntity.getStatusCode().is2xxSuccessful()){
            return  responseEntity.getBody();
        }else{
            return  "1111";
        }
    }

}
